<?php 
namespace Common\Model;
use Think\Auth;
use Think\Model;
class GoodsModel extends Model{
	protected $table;
	protected $pre;
	protected $goodstable;

	public function __construct($goodstable,$pre =''){
		parent::__construct();
		$this->pre = empty($pre) ? C('DB_PREFIX') : $pre;
		$this->table = $goodstable;
		$this->goodstable = $this -> $pre.$goodstable; 
	}

	public function addGood($data){
		$checkInfo = $this -> checkInfo($data);
		if($checkInfo !== true){
			return $checkInfo;
		}
		$goodsdata = $this -> formatInfo($data);
		  //dump($goodsdata);exit;
		$goods = M($this->table);
		$mid = $goods -> add($goodsdata,[],true);
		if(!$mid){
			return $this -> returnInfo('','失败了！加油');
		}
		return $this -> returnInfo('','成功了！',1);
	}

	public function checkInfo($data){
		if(empty($data['title'])){
			return $this -> returnInfo('','产品标题不能为空！');
		}	
		if(empty($data['signum'])){
			return $this -> returnInfo('','产品编号不能为空！');
		}
		if(empty($data['keywords'])){
			return $this -> returnInfo('','关键词不能为空！');
		}
		if(empty($data['description'])){
			return $this -> returnInfo('','描述不能为空！');
		}
		if(strlen($data['sort']) > 6){
			return $this -> returnInfo('','排序不符合规则！');
		}
		$goods = M($this->table);
		if(empty($data['id'])){
			$goodsconut = $goods -> field('id') -> where(['title' => $data['title'],'signum' => $data['signum']]) -> count();
			if($goodsconut > 0){
				return $this -> returnInfo('','产品已存在！');
			}
		}
		return true;
	}

	public function returnInfo($data,$msg = '',$status = 0){
		return ['status' => $status, 'mess' => $msg, 'data' => $data];
	}

	public function formatInfo($data){
		$id = empty($data['id']) ? 0 : $data['id'];
		$cat_id = empty($data['cat_id']) ? 0 : $data['cat_id'];
		$imgPath = empty($data['litpic']) ? '' : $data['litpic'];
		$years = empty($data['years']) ? '' : $data['years'];
		$size = empty($data['size']) ? '' : $data['size'];
		$weight = empty($data['weight']) ? '' : $data['weight'];
		$author = empty($data['author']) ? '' : $data['author'];
		$mater = empty($data['mater']) ? '' : $data['mater'];
		$content = empty($data['content']) ? '' : $data['content'];
		$hits = empty($data['hits']) ? 0 : $data['hits'];
		$sort = empty($data['sort']) ? 0 : $data['sort'];
		$addman = session('user.account');
		// dump($imgPath);exit;
		$dataInfo = ['id'=>$id,'cat_id'=>$cat_id,'title'=>$data['title'],'img'=>$imgPath,'signum'=>$data['signum'],'years'=>$years,'size'=>$size,'weight'=>$weight,'author'=>$author,'mater'=>$mater,'keywords'=>$data['keywords'],'description'=>$data['description'],'content'=>$content,'hits'=>$hits,'addman'=>$addman,'sort'=>$sort];
		// dump($dataInfo);exit;
		return $dataInfo;
	}

	public function goods_list($id,$field = 'id',$alias,$join,$sql,$frow,$num,$order = 'id asc'){
		$goods = M($this->table);
		$list = $goods -> field($field);
		$list = $list -> alias($alias) -> join($join);
		if(!empty($id)){
			$list = $list -> where(['id' => $id]);
		}else{
			$list = $list -> where($sql); 
		}
		$list = $list -> limit($frow,$num);	
		$list = $list -> order($order) -> select();

		if(empty($list)){
			return $this -> returnInfo('','没有数据！',0);
		}else{
			return $this -> returnInfo($list,'成功找到数据',1);
		}		
	}

	public function Delgoods($id){
		if(empty($id)){
			$this -> returnInfo('','操作异常！');
		}
		$is_del = M($this->table) ->where(['id' => $id]) -> delete(); 
		if(!$is_del){
			return $this -> returnInfo('','删除失败！');
		}
		return $this -> returnInfo('','删除成功！',1);
	}


}